package com.xidian.zhiancloud.service.exam.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.exam.TExamPlanMapper;
import com.xidian.zhiancloud.model.exam.PlanCount;
import com.xidian.zhiancloud.model.exam.TExamPlan;
import com.xidian.zhiancloud.model.exam.TrainPlan;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.exam.ITExamPlanService;
import com.xidian.zhiancloud.util.api.DateUtils;

/**
 * 考试计划Service业务层处理
 * 
 * @author zhouweitao
 * @date 2021-03-08
 */
@Service
public class TExamPlanServiceImpl implements ITExamPlanService 
{
    @Autowired
    private TExamPlanMapper tExamPlanMapper;

    /**
     * 查询考试计划
     * 
     * @param id 考试计划ID
     * @return 考试计划
     */
    @Override
    public TExamPlan selectTExamPlanById(Long id)
    {
        return tExamPlanMapper.selectTExamPlanById(id);
    }

    /**
     * 查询考试计划列表
     * 
     * @param tExamPlan 考试计划
     * @return 考试计划
     */
    @Override
    public Page<TExamPlan> selectTExamPlanList(TExamPlan tExamPlan)
    {
    	PageHelper.startPage(tExamPlan.getPageNum()-1, tExamPlan.getPageSize());
        return tExamPlanMapper.selectTExamPlanList(tExamPlan);
    }

    /**
     * 新增考试计划
     * 
     * @param tExamPlan 考试计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTExamPlan(TExamPlan tExamPlan)
    {
        tExamPlan.setCreateTime(DateUtils.getNowDate());
        int result =  tExamPlanMapper.insertTExamPlan(tExamPlan);
        if(tExamPlan.getUsers() !=null && tExamPlan.getUsers().size()>0) {
        	for (int i = 0; i < tExamPlan.getUsers().size(); i++) {
        		tExamPlan.getUsers().get(i).setPlanId(tExamPlan.getId());
			}
        	tExamPlanMapper.insertTExamPlanUser(tExamPlan.getUsers());
        }
        
        return result;
    }

    /**
     * 修改考试计划
     * 
     * @param tExamPlan 考试计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTExamPlan(TExamPlan tExamPlan)
    {
        tExamPlan.setUpdateTime(DateUtils.getNowDate());
        int result =tExamPlanMapper.updateTExamPlan(tExamPlan);
        tExamPlanMapper.deletePlanUserbyId(tExamPlan.getId());
        if(tExamPlan.getUsers() !=null && tExamPlan.getUsers().size()>0) {
        	for (int i = 0; i < tExamPlan.getUsers().size(); i++) {
        		tExamPlan.getUsers().get(i).setPlanId(tExamPlan.getId());
			}
        	tExamPlanMapper.insertTExamPlanUser(tExamPlan.getUsers());
        }
        
        return result;
    }

    /**
     * 批量删除考试计划
     * 
     * @param ids 需要删除的考试计划ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTExamPlanByIds(Long[] ids)
    {
    	tExamPlanMapper.deletePlanUser( ids);
        return tExamPlanMapper.deleteTExamPlanByIds(ids);
    }

    /**
     * 删除考试计划信息
     * 
     * @param id 考试计划ID
     * @return 结果
     */
    @Override
    public int deleteTExamPlanById(Long id)
    {
        return tExamPlanMapper.deleteTExamPlanById(id);
    }

	@Override
	public PlanCount getCountPlanAndEaxm(Long id) {
		// TODO Auto-generated method stub
		return tExamPlanMapper.getCountPlanAndEaxm(id);
	}

	@Override
	public Page<TrainPlan> getExamByUser(BaseReqQuery base, Long id) {
		// TODO Auto-generated method stub
		PageHelper.startPage(base.getPageNum()-1, base.getPageSize());
        return tExamPlanMapper.getExamByUser(base,id);
	}
}
